<?php
/**
 * See the enclosed file license.txt for licensing information.
 * If you did not receive this file, get it at http://www.fsf.org/copyleft/gpl.html
 *
 * @copyright   The XOOPS project http://www.xoops.org/
 * @license     http://www.fsf.org/copyleft/gpl.html GNU General Public License (GPL)
 * @package     installer
 * @since       2.3.0
 * @author      Haruki Setoyama  <haruki@planewave.org>
 * @author      Kazumi Ono <webmaster@myweb.ne.jp>
 * @author      Skalpa Keo <skalpa@xoops.org>
 * @author      Taiwen Jiang <phppp@users.sourceforge.net>
 * @author      DuGris (aka L. JEN) <dugris@frxoops.org>
 * @version     $Id: installwizard.php 1429 2008-04-02 12:27:19Z phppp $
**/

if ( !defined('XOOPS_INSTALL') ) { die('XOOPS Installation wizard die'); }

class XoopsInstallWizard {
	var $pages = array();
	var $titles = array();
	var $currentPage = 0;
	var $language = 'english';

	function xoInit() {
		if ( @empty( $_SERVER['REQUEST_URI'] ) ) {
			$_SERVER['REQUEST_URI'] = $_SERVER['PHP_SELF'];
		}

		// Load the main language file
		$this->initLanguage( !@empty( $_COOKIE['xo_install_lang'] ) ? $_COOKIE['xo_install_lang'] : 'english' );

		if ( !$this->checkAccess() ) {
			return false;
		}

		// Setup pages
		$this->pages        = array(
				'langselect', 'start', 'modcheck',
				'pathsettings',
				'dbconnection',
				'dbsettings',
				'configsave',
				'tablescreate',
				'siteinit',
				'tablesfill',
				'configsite',
				'theme',
				'moduleinstaller',
				'end',
		);
		$this->pagesNames    = array(
				LANGUAGE_SELECTION, INTRODUCTION, CONFIGURATION_CHECK,
				PATHS_SETTINGS,
				DATABASE_CONNECTION,
				DATABASE_CONFIG,
				CONFIG_SAVE,
				TABLES_CREATION,
				INITIAL_SETTINGS,
				DATA_INSERTION,
				CONFIG_SITE,
				THEME,
				MODULES,
				WELCOME,
		);
		$this->pagesTitles    = array(
				LANGUAGE_SELECTION_TITLE, INTRODUCTION_TITLE, CONFIGURATION_CHECK_TITLE,
				PATHS_SETTINGS_TITLE,
				DATABASE_CONNECTION_TITLE,
				DATABASE_CONFIG_TITLE,
				CONFIG_SAVE_TITLE,
				TABLES_CREATION_TITLE,
				INITIAL_SETTINGS_TITLE,
				DATA_INSERTION_TITLE,
				CONFIG_SITE_TITLE,
				THEME_TITLE,
				MODULES_TITLE,
				WELCOME_TITLE,
		);

		include_once './config/config.php';

		// Database type
		$this->db_types  = $db_types;

		// setup config site info
		$this->conf_names  = $conf_names;

		// languages config files
		$this->language_files = $language_files;

		// extension_loaded
		$this->extensions = $extensions;

		// Modules to be installed by default
		$this->modules = $modules;

		// xoops_lib, xoops_data directories
		$this->xoopsPathDefault = $xoopsPathDefault;

		// writable xoops_lib, xoops_data directories
		$this->dataPath = $dataPath;

		// Protector default trust_path
		$this->trust_path = isset($trust_path) ? $trust_path : false;

		// Writable files and directories
		$this->writable = $writable;

		$this->actionName = $pagename = preg_replace('~(page_)(.*)~','$2', basename( $_SERVER['PHP_SELF'], ".php" ) );
		$this->setPage( $pagename );
		// Prevent client caching
		header( "Cache-Control: no-store, no-cache, must-revalidate", false );
		header( "Pragma: no-cache" );
		return true;
	}

	function checkAccess() {
		if ( $_SERVER['REQUEST_METHOD'] && $_SERVER['REQUEST_METHOD'] == 'POST' ) {
			if ( isset($_POST['uname']) && isset($_POST['pass']) ) {
				include 'include/checklogin.php';
			}
		}

		if ( defined( 'XOOPS_MAINFILE_INCLUDED') && ( !isset($_SESSION['UserLogin']) || !$_SESSION['UserLogin']) ) {
			include 'include/login.php';
		}
		return true;
	}

	function loadLangFile( $file ) {
		if ( file_exists( "./language/$this->language/$file.php" ) ) {
			include_once "./language/$this->language/$file.php";
		} else {
			include_once "./language/english/$file.php";
		}
	}

   function initLanguage( $language ) {
   	$language = preg_replace("/[^a-z0-9_\-]/i", "", $language);
   	if ( !file_exists( "./language/$language/install.php" ) ) {
   		$language = 'english';
   	}
   	$this->language = $language;
   	$this->loadLangFile( 'install' );
   }

   function setPage( $page ) {
   	if ( (int)$page && $page >= 0 && $page < count($this->pages) ) {
   		$this->currentPageName = $this->pages[ $page ];
   		$this->currentPage = $page;
   	} elseif ( false !== ( $index = array_search( $page, $this->pages ) ) ) {
   		$this->currentPageName =  $this->pages[ $index ];
   		$this->currentPage = $index;
   	} else {
   		return false;
   	}

		if ( $this->currentPage > 0 && !isset($_SESSION['language']) ) {
			header('Location: index.php');
		}

   	return $this->currentPage;
   }

   function baseLocation() {
   	$proto    = ( @$_SERVER['HTTPS'] == 'on') ? 'https' : 'http';
   	$host    = $_SERVER['HTTP_HOST'];
   	$base    = substr( $_SERVER['PHP_SELF'], 0, strrpos( $_SERVER['PHP_SELF'], '/' ) );
   	return "$proto://$host$base";
   }

   function pageURI( $page ) {
   	if ( !(int)$page{0} ) {
   		if ( $page{0} == '+' ) {
   			$page = $this->currentPage + substr( $page, 1 );
   		} elseif ( $page{0} == '-' ) {
   			$page = $this->currentPage - substr( $page, 1 );
   		} else {
   			$page = (int)array_search( $page, $this->pages );
   		}
   	}
   	if (!isset($this->pages[$page]) && defined("XOOPS_URL")) {
   		return XOOPS_URL;
   	}
   	$page = $this->pages[$page];
   	return $this->baseLocation() . "/page_$page.php";
   }

   function redirectToPage( $page, $status = 303, $message = 'See other' ) {
   	$location = $this->pageURI( $page );
   	$proto = !@empty($_SERVER['SERVER_PROTOCOL']) ? $_SERVER['SERVER_PROTOCOL'] : 'HTTP/1.1';
   	header( "$proto $status $message" );
   	//header( "Status: $status $message" );
   	header( "Location: $location" );
   }

	function ext_loaded() {
		foreach ( $this->extensions as $ext => $value) {
			if ( extension_loaded( $ext ) ) {
				if ( is_array($value[0] ) ) {
					$this->extensions[$ext][] = xoDiag( 1, implode( ',', $value[0] ) );
				} else {
					$this->extensions[$ext][] = xoDiag( 1, $value[0] );
				}
			} else {
				$this->extensions[$ext][] = xoDiag( 0, NONE );
			}
		}
	}

	function loadConfigLangFile() {
		if ( !@include_once "../modules/system/language/$this->language/admin.php" ) {
			include_once "../modules/system/language/english/admin.php";
		}

		if ( !@include_once "../modules/system/language/$this->language/admin/preferences.php" ) {
			include_once "../modules/system/language/english/admin/preferences.php";
		}
		foreach( $this->language_files as $file) {
			if ( !@include_once "../language/$this->language/$file.php" ) {
				include_once "../language/english/$file.php";
			}
		}
	}

	function loadConfigInfo() {
		$config_handler = xoops_gethandler('config');
		$criteria = new CriteriaCompo();
		$criteria->add(new Criteria('conf_modid', 0));

		$criteria2 = new CriteriaCompo();
		foreach ($this->conf_names as $conf_name) {
   		$criteria2->add(new Criteria('conf_name', $conf_name), 'OR');
		}
		$criteria->add($criteria2);
		$criteria->setSort('conf_catid ASC, conf_order ASC');
		$this->config = $config_handler->getConfigs($criteria);

		include './include/createconfigform.php';
		$this->form = createConfigform($this->config);
	}

	function CreateForm() {
		$hidden = '';
		$ret = '';

		foreach ($this->form as $form) {
			$ret .= "<fieldset><legend>" . $form->getTitle() . "</legend>\n";

			foreach( $form->getElements() as $ele) {
				if (is_object($ele)) {
					if ( !$ele->isHidden() ) {
						if ( ($caption = $ele->getCaption()) != '' ) {
							$name = $ele->getName();
							$ret .= "<label for='" . $ele->getName() . "'>" . $caption . "</label>";
							if ( ($desc = $ele->getDescription()) != '' ) {
								$ret .= "<div class='xoform-help'>";
								$ret .= $desc;
								$ret .= "</div>";
							}
							$ret .= "</label>\n";
						}
						$ret .= $ele->render() . "\n";
					} else {
						$hidden .= $ele->render() . "\n";
					}
				}
			}
			$ret .= "</fieldset>\n" . $hidden. "\n" . $form->renderValidationJS( true ) ;
		}
		return $ret;
	}

	function loadModuleLangFile() {
		if ( !@include_once "../language/$this->language/global.php" ) {
			include_once "../language/english/global.php";
		}

		if ( !@include_once "../modules/system/language/$this->language/admin/modulesadmin.php" ) {
			include_once "../modules/system/language/english/admin/modulesadmin.php";
		}
	}

	function loadThemeInfo() {
		$config_handler = xoops_gethandler('config');
		$criteria = new CriteriaCompo();
		$criteria->add(new Criteria('conf_modid', 0));
		$criteria->add(new Criteria('conf_name', 'theme_set'));

		$this->config = $config_handler->getConfigs($criteria);
		include './include/createconfigform.php';
		$this->form = createThemeform($this->config[0]);
	}
}
?>